package com.example.myapplication.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.myapplication.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserListView extends BaseAdapter {
    private Context context;
    private List<Map> listMap;

    public UserListView(Context context, List<Map> listMap) {
        this.context = context;
        this.listMap = listMap;
    }

    @Override
    public int getCount() {
        return listMap.size();
    }

    @Override
    public Object getItem(int position) {
        return listMap.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    static class UserView{
        private TextView tv1,tv2,tv3,tv4,tv5,tv6;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        UserView userView = null;


        if(userView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.user_view, null);
            userView = new UserView();
            userView.tv1 = convertView.findViewById(R.id.tv1);
            userView.tv2 = convertView.findViewById(R.id.tv2);
            userView.tv3 = convertView.findViewById(R.id.tv3);
            userView.tv4 = convertView.findViewById(R.id.tv4);
            userView.tv5 = convertView.findViewById(R.id.tv5);
            userView.tv6 = convertView.findViewById(R.id.tv6);
            convertView.setTag(userView);
            Log.d("userView====================", userView.toString());
        }else {
            userView = (UserView) convertView.getTag();
        }
        userView.tv1.setText(listMap.get(position).get("id")==null?null: listMap.get(position).get("id").toString());
        userView.tv2.setText(listMap.get(position).get("name")==null?null: listMap.get(position).get("name").toString());
        userView.tv3.setText(listMap.get(position).get("age")==null?null: listMap.get(position).get("age").toString());
        userView.tv4.setText(listMap.get(position).get("phone")==null?null: listMap.get(position).get("phone").toString());
        userView.tv5.setText(listMap.get(position).get("create_time")==null?null: listMap.get(position).get("create_time").toString());
        userView.tv6.setText(listMap.get(position).get("update_time")==null?null: listMap.get(position).get("update_time").toString());
        return convertView;
    }
}
